// pages/timecard/normal/normal.js
//获取应用实例
var app = getApp()
var util = require('../../../utils/util.js')
var amapFile = require('../../../utils/amap-wx.js');

const AV = require('../../../utils/av-weapp-min');
const Check = require('../../../model/check')

Page({
  data:{
    markers:null,//风车
    ifdk:1,//是否可以打卡
    tname:"",//教师名称
    type:0,//当前打卡类型
    khblock:1,//课后弹窗是否隐藏
    star:5,//评星
    latitude: 0,
    longitude: 0,
    displayTime: null,
    uindex: null,
    index: 0,
    title: null,
    lont:null,
    lat:null,
    checkType:null,
    // checkType: [
    //   [{id: "1", msg: "上班"}, {id: "2", msg: "下班"}, {id: "3", msg: "上课"}, {id: "4", msg: "下课"}]
      
    // ],      
    loading: false, // 更新地理位置加载状态
    checkMode: {},
    UI: [
      {checkType: "打卡目的", current: "当前选择", locName: "位置名称", locDesc: "详细位置", locNameContent: "等待获取", locDescContent: "等待获取", locButton: "更新定位", submitButton: "提交"},
      {checkType: "Type", current: "Current", locName: "Location", locDesc: "Detail", locNameContent: "Waiting", locDescContent: "Waiting", locButton: "Update Location", submitButton: "Submit"},
      {checkType: "打刻種類", current: "選択項目", locName: "現在場所", locDesc: "詳細位置", locNameContent: "取得待ち", locDescContent: "取得待ち", locButton: "場所再取得", submitButton: "打刻"}
      ],
      stars: [{
        lightImg: '../../../image/stara.png',
        blackImg: '../../../image/star.png',
        flag: 1,
      }, {
        lightImg: '../../../image/stara.png',
        blackImg: '../../../image/star.png',
        flag: 1,
      }, {
        lightImg: '../../../image/stara.png',
        blackImg: '../../../image/star.png',
        flag: 1,
      }, {
        lightImg: '../../../image/stara.png',
        blackImg: '../../../image/star.png',
        flag: 1,
      }, {
        lightImg: '../../../image/stara.png',
        blackImg: '../../../image/star.png',
        flag: 1,
      }]
  },
  //获取markers
  getMarkers:function(e){
    var that = this;
    var mks = [
      {"id":1,"latitude":45.7554,"longitude":"126.6634","iconPath":"../../../image/fc.png","title":"风车1","callout":{"content":"风车1","display":"ALWAYS"}}
      ,{"id":2,"latitude":45.7545,"longitude":"126.6424","iconPath":"../../../image/fc.png","title":"风车2","callout":{"content":"风车2","display":"ALWAYS"}}
      ,{"id":3,"latitude":45.7555,"longitude":"126.6484","iconPath":"../../../image/fc.png","title":"风车2","callout":{"content":"风车3","display":"ALWAYS"}}
  ];
    that.setData({
      markers:mks,
    })
  },

  // 选择评价星星
  starClick: function (e) {
    var that = this;
    for (var i = 0; i < that.data.stars.length; i++) {
      var allItem = 'stars[' + i + '].flag';
      that.setData({
        [allItem]: 2
      })
    }
    var index = e.currentTarget.dataset.index;
    console.log("i:"+index);
    for (var i = 0; i <= index; i++) {
      var item = 'stars[' + i + '].flag';
      that.setData({
        [item]: 1,
        star:index+1,
      })
    }
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({      
      loading: true
    })
    var selectedLanguage = app.globalData.settings.language;
    var toastTitle = ['定位成功', 'Got Location', '取得完了'][selectedLanguage];
    var that = this;
    var ui = that.data.UI;

    //根据时间判断打卡形式
    wx.request({
      url: app.globalData.serverPath + 'miniuser/work/getWorkType' ,
      data:{
        openid:app.globalData.openid,
      },
      success:function(res) {
        var info = res.data.data;
        console.log(info);
        that.setData({
          checkType:info,
          type:res.data.msg,
        })
      }
    })

    this.mapCtx = wx.createMapContext("mapid");
    this.mapCtx.moveToLocation();
    this.getMarkers();
    wx.getLocation({
      success (res) {
        console.log("定位");
        console.log(res);
        that.setData({
          lont:res.longitude,
          lat:res.latitude,
        })
      }
    })

    // var amap = new amapFile.AMapWX({key:'e441fceb8e63900bbe6a292d670cad72'});
    
    // amap.getRegeo({      
    //   success: function(data){
    //     //成功回调
    //     wx.showToast({
    //       title: toastTitle,
    //       icon: 'success',
    //       duration: 1000
    //     })
    //     // 改写UI，反映在视图层
    //     console.log(data[0])
    //     ui[selectedLanguage].locNameContent =  data[0].name
    //     ui[selectedLanguage].locDescContent =  data[0].desc
    //     var lat = data[0].latitude;
    //     var lon = data[0].longitude; 
    //     that.setData({
    //       UI: ui,          
    //       latitude: lat,
    //       longitude: lon,
    //       loading: false
    //     })  
    //   }
    // })
  },
  onReady:function(){
    // 页面渲染完成
    this.setData({
        displayTime: util.currentTime()    
      });
  },
  onShow:function(){
    // 设置app语言的全局变量  
    var selectedLanguage = app.globalData.settings.language;
    console.log('Current Language:' + selectedLanguage + ' (0: ZH-ch 1: ENG 2:JP)');
    var title = ["打卡", "Timecard", "打刻"][selectedLanguage];
    this.setData({
      uindex: selectedLanguage,
      title: title
    })
    // 时间显示
    var that = this;
    setInterval(function(){
      that.setData({
        displayTime: util.currentTime()    
      });
    }, 1000)
  },
  bindPickerChange:function(e){
    // 页面隐藏
    this.setData({
      index: e.detail.value
    })
  },
  // relocate: function(){
  //   this.setData({      
  //     loading: true
  //   })
  //   var selectedLanguage = app.globalData.settings.language;
  //   var toastTitle = ['定位成功', 'Got Location', '取得完了'][selectedLanguage];
  //   var that = this;
  //   var ui = that.data.UI
  //   var amap = new amapFile.AMapWX({key:'8ebbe699d71eed6674889848604e411a'});
    
  //   amap.getRegeo({      
  //     success: function(data){
  //       console.log(data)
  //       //成功回调
  //       wx.showToast({
  //         title: toastTitle,
  //         icon: 'success',
  //         duration: 1000
  //       })
  //       // 改写UI，反映在视图层
  //       ui[selectedLanguage].locNameContent =  data[0].name
  //       ui[selectedLanguage].locDescContent =  data[0].desc
  //       that.setData({
  //         UI: ui,
  //         loading: false,
  //         latitude: data[0].latitude,
  //         longitude: data[0].longitude,           
  //       })  
  //     }
  //   })
    
  // },
  //提交课后问题
  subkh:function(e){
    var that = this;

    if(that.data.ifdk!=1){
      wx.showModal({
        title:'请勿重复操作！',
        showCancel:false,
      })
      return false;
    }
    //隐藏弹窗
    that.setData({
      khblock:1,
    })

    var tname=that.data.tname;
    var type = that.data.type;
    var star=that.data.star;
    console.log(tname);
    wx.request({
      url: app.globalData.serverPath + 'miniuser/work/doWorkLog' ,
      data:{
        openid:app.globalData.openid,
        type:type,
        point:that.data.lont+","+that.data.lat,
        tname:tname,
        star:star,
      },
      success:function(res) {
        console.log("打卡结果");
        console.log(res);
        if(res.data.code=='00'){
          wx.showModal({
            title:'打卡成功',
            showCancel:null,
          })
          that.setData({
            ifdk:0,
          })
        }else{
          wx.showModal({
            title:'打卡失败',
            content:'res.data.msg',
            showCancel:null,
          })
        }
      }
    })
  },
  setValue:function(e) {
    var that = this;
    that.setData({
      tname:e.detail.value,
    })
  },
  formSubmit: function(e){
    var that = this;
    if(that.data.ifdk!=1){
      wx.showModal({
        title:'请勿重复操作！',
        showCancel:false,
      })
      return false;
    }
    if(that.data.lat==null){
      wx.showModal({
        title:'打卡失败',
        content:'定位尚未成功，请稍等！',
        showCancel:false,
      })
      return false;
    }
    if(that.data.type==0){
      wx.showModal({
        title:'您当前无可执行打卡操作！',
        showCancel:false,
      })
      return false;
    }
    var type = e.detail.value.type;
    console.log("type is :"+type);
    
    //如果是下课打卡，则弹出课后问题
    if(type=="2"){
      that.setData({
        khblock:0,
      })
      return false;
    }
    
    wx.request({
      url: app.globalData.serverPath + 'miniuser/work/doWorkLog' ,
      data:{
        openid:app.globalData.openid,
        type:type,
        point:that.data.lont+","+that.data.lat,
      },
      success:function(res) {
        console.log("打卡结果");
        console.log(res);
        if(res.data.code=='00'){
          wx.showModal({
            title:'打卡成功',
            showCancel:null,
          })
          that.setData({
            ifdk:0,
          })
        }else{
          wx.showModal({
            title:'打卡失败',
            content:'res.data.msg',
            showCancel:null,
          })
        }
      }
    })

  }
})